package com.example.gesture_test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.util.Log;


public class FileManager{

	private FileWriter fw;
	private File records;
	private String filePath = null;
	
	private FileManager()
	{
		
	}
	public FileManager(Context c)
	{
		filePath = c.getFilesDir().getAbsolutePath()+"/records.txt";
		Log.i("stack",filePath);
		records = new File(filePath);
		if (records.exists() == false) {
			try
			{ 
				Log.i("stack", "creating a file");

				records.createNewFile();
				fw = new FileWriter(records);
				fw.write("This is a guideline\nFor add, drag down");
				fw.flush();
				fw.close();
			}
			catch (IOException e)
			{
				//tv1.setText(e + "Failed to create file");
			}
		} 
		
	}

	//	@Override
	//    public void onCreate(Bundle savedInstanceState)
	//	{
	//        super.onCreate(savedInstanceState);
	//        setContentView(R.layout.main);
	//		
	//		btn1 = (Button) findViewById(R.id.btn1);
	//		btn1.setOnClickListener(btn1OnClick);
	//		btn2 = (Button) findViewById(R.id.btn2);
	//		btn2.setOnClickListener(btn2OnClick);
	//		
	//		tv1 = (TextView) findViewById(R.id.tv1);
	//		
	//	//	edit1 = (EditText) findViewById(R.id.edit1);
	//		edit2 = (EditText) findViewById(R.id.edit2);
	//		Log.i("stack","/a.txt123");
	//    }
	//	OnClickListener btn1OnClick = new OnClickListener() {
	//		public void onClick(View v) {
	//			Log.i("stack","/a.txt");
	//			try {
	//				write();
	//			} catch (IOException e) {
	//				// TODO Auto-generated catch block
	//				e.printStackTrace();
	//			}
	//		}
	//	};
	//	OnClickListener btn2OnClick = new OnClickListener() {
	//		public void onClick(View v) {
	//			try {
	//				read(path);
	//			} catch (IOException e) {
	//				// TODO Auto-generated catch block
	//				e.printStackTrace();
	//			}
	//		}
	//	};

	private void read(String filename) throws IOException {

		File f = new File(filename);
		FileInputStream fis = new FileInputStream(f);

		FileOutputStream fos = null;
		BufferedReader br = new BufferedReader(new InputStreamReader(fis,"utf-8"));
		String line = br.readLine();
		while(line!=null)
		{
			Log.i("stack", line);
			line = br.readLine();
		}

	}

	public String readToString()
	{
		String con = "";

		File file = new File(filePath);
		
		FileInputStream fis;
		InputStreamReader isr;
		try {
			fis = new FileInputStream(file);
			isr = new InputStreamReader(fis);
			BufferedReader br = new BufferedReader(isr);
			String tmp = null;
			
			while((tmp=br.readLine())!=null)
			{
				//Log.i("stack", tmp);
				con += tmp+"\n";
			}
			Log.i("stack", con);
			fis.close();
			isr.close();
			br.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			Log.i("stack", e.getMessage());
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			Log.i("stack", e.getMessage());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.i("stack", e.getMessage());
		}

		return con;
	}

	public boolean refresh(ArrayList<String> list)
	{
		File file = new File(filePath);
		if (file.exists() == false)
			return false;
		try{
			fw = new FileWriter(file);
			int length = list.size();
			String tmp;
			for(int i=0;i<length;i++)
			{
				tmp = list.get(i);
				fw.write(tmp+"\n");
			}
			fw.flush();
			fw.close();
		}	catch (FileNotFoundException e) {
			//tv1.setText(e + "File not found");
		} catch (IOException e) {
			//tv1.setText(e + "Failed to save");
		}
		return true;
		

	}
//	private void write(String filePath) throws IOException {
//		String dirPath = getFilesDir().getAbsolutePath();
//		File file = new File(filePath);
//		Log.i("stack", getFilesDir().getAbsolutePath());
//
//		if (file.exists() == false) {
//			try
//			{ 
//				FileOutputStream fos = openFileOutput("test.txt", Context.MODE_WORLD_READABLE);
//				String str = "Android File IO Test";
//				fos.write(str.getBytes());   
//				Log.i("stack", "creating a file");
//
//				file.createNewFile();
//			}
//			catch (IOException e)
//			{
//				//tv1.setText(e + "Failed to create file");
//			}
//		} else {
//			try{
//				String str =  edit2.getText().toString();
//				FileWriter fw = new FileWriter(file,true);
//				fw.append(str);
//				fw.flush();
//				fw.close();
//			}	catch (FileNotFoundException e) {
//				//tv1.setText(e + "File not found");
//			} catch (IOException e) {
//				//tv1.setText(e + "Failed to save");
//			}
//		}


		//path = dirPath+"/a.txt";

//	}
}
